package seismosurfer.util;

/**
 * This class factorizes code needed for
 * parameter checking.
 *
 */
public class Assert {
    
    /**
     * If the condition is not true, throws a RuntimeException 
     * which contains the given message.
     * 
     * @param condition the boolean expression to be checked
     * @param message the String that will be included in the exception
     */
    public static void isTrue(boolean condition, String message) {
        if (!condition)
            throw new SeismoException(message);
    }
    
    /**
     * Throws a NullPointerException if the given
     * object is null.
     * 
     * @param o the Object to be checked 
     */
    public static void notNull(Object o) {
        notNull(o, "");
    }
    
    /**
     * Throws a NullPointerException if the given
     * object is null.
     * 
     * @param o the Object to be checked
     * @param message the String that will be included in the exception
     */
    public static void notNull(Object o, String message) {
        if (o == null)
            throw new NullPointerException(message);
    }
    
    /**
     * Throws a RuntimeException if the given
     * text has not the specified length.
     * 
     * @param text the String to be checked
     * @param length the expected length of the text
     */
    public static void length(String text, int length) {
        if (text.length() != length) {
            throw new SeismoException("Text \"" + text
                    + "\" has not the expected length.");
        }
    }    
}